<?php
include '../data/rfq_dal.php';
include_once '../entity/rfq_entity.php';

class RFQMng
{
    function update($rfqEntity)
    {
        $rfqDAL = new RFQDAL();
        $rfqEntity->requiredNDA = true;
        $rfqEntity->showPrice = true;
        if ($result = $rfqDAL->update($rfqEntity->id, $rfqEntity->categoryID, $rfqEntity->marketID,  $rfqEntity->requireQC, $rfqEntity->process, $rfqEntity->quantity,
                                      $rfqEntity->measure ,  $rfqEntity->annualUsage,  $rfqEntity->neededDate,  $rfqEntity->awardDate,  $rfqEntity->deliveryDate,
                                      $rfqEntity->paymentTerm,  $rfqEntity->paymentMethod ,  $rfqEntity->referencePrice,  $rfqEntity->referencePrice,  $rfqEntity->targetPrice ,
                                      $rfqEntity->showPrice, $rfqEntity->folder,  $rfqEntity->shippingCost,  $rfqEntity->instructions,  $rfqEntity->rfqIntent,  $rfqEntity->usageDetails,
                                      $rfqEntity->industry,  $rfqEntity->requiredNDA,  $rfqEntity->files,  $rfqEntity->inTime,  $rfqEntity->inUser)) {
            $row = $result->fetch_array();                   
            
        }
        return $row[0];
    }

    function get($id)
    {
        $where = "id = " . $id;
        $rfqDAL = new RFQDAL();
        if ($result = $rfqDAL->get($where)) {
            $row  = $result->fetch_assoc();
            $rfqEntity = new RFQEntity();
            
            foreach($row as $key => $value) {
               $rfqEntity->$key = $value;
           }
            return $rfqEntity;
        } else {
            return false;
        }
    }

    function getAll($status, $start, $pageSize, $sortField, $sortDirection)
    {
        $orderBy = "WHERE status = " . $status . " ORDER BY " . $sortField . " " . $sortDirection . " LIMIT " . $start . ", " . ($pageSize * ($start + 1));
        $rfqDAL = new RFQDAL();
        if ($result = $rfqDAL->getAll($orderBy)) {
            while ($row = $result->fetch_array()) {
                $row  = $result->fetch_array();
                $rfqEntity = new RFQEntity();

                foreach($row as $key => $value) {
                    $rfqEntity->$key = $value;
                }
                $rfqList[] = $rfqEntity;
            }
            return $rfqList;
        } else {
            return false;
        }
    }

    function delete($id)
    {
        $rfqDAL = new RFQDAL();
        return $rfqDAL->delete($id);
    }

    function supplierGetAll($categoryID, $start, $pageSize, $sortField, $sortDirection)
    {
        $orderBy = "WHERE categoryID = ". $categoryID ." ORDER BY " . $sortField . " " . $sortDirection . " LIMIT " . $start . ", " . ($pageSize * ($start + 1));
        $rfqDAL = new RFQDAL();
        if ($result = $rfqDAL->getAll($orderBy)) {
            while ($row = $result->fetch_array()) {
                $row  = $result->fetch_array();
                print_r($row);
                $rfqEntity = new RFQEntity();

                foreach($row as $key => $value) {
                    $rfqEntity->$key = $value;
                }
                $rfqList[] = $rfqEntity;
            }
            return $rfqList;
        } else { echo "false";
            return false;
        }
    }
}

?>
